require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/iconfont.css");
require("./login.less");
// 引入axios
const axios = require("axios");
// 引入工具js

const dom = require("../../utils/dom.js");
document.addEventListener("DOMContentLoaded", function () {
	dom.get("#regBtn").onclick = function () {
		let error = dom.get("#error");

		// 手机号验证
		let tel = dom.get("#tel").value;
		let regxp = /^1[3456789]\d{9}$/i;
		if (regxp.test(tel)) {
			error.textContent = "";
		} else {
			error.textContent = "请输入正确的电话号码";
			return;
		}

		// 密码验证
		let pwd = dom.get("#pwd").value;
		if (pwd.length >= 6) {
			error.textContent = "";
		} else {
			error.textContent = "请输入6位数以上密码";
			return;
		}

		// 都通过了验证,再发送请求
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/login", {
				account: tel,
				password: pwd,
			})
			.then(res => {
				console.log(res);
				if (res.data.errno === 0) {
					localStorage.setItem("token", res.data.data.token);
					alert("登陆成功哦!!");
					location.href = "first.html";
				} else {
					alert("很抱歉,登陆失败了!!");
				}
			});
	};
});
